<template>
  <SearchBox ref="searchRef" :searchValue="searchValue" @search="search" />
  <tableData :tableList="tableList" @changePagination="changePagination" :pagination="pagination" />
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import tableData from './components/orderView/tableData.vue'
import type { TTable, TSearch } from './components/orderView/type.dict'
import SearchBox from './components/orderView/searchBox.vue'

import cloud from '@/utils/cloud'
const searchRef = ref()

//初始化SDK实例
const tableList = ref<TTable[]>([])

const pagination = ref<{
  total: number
  current: number
  pageSize: number
}>({
  total: 0,
  current: 0,
  pageSize: 10,
})
const searchValue = ref<TSearch>({
  status: '',
})
// 搜索操作
const search = (searchVal: TSearch) => {
  searchValue.value = { ...searchValue.value, ...searchVal }
  pagination.value.current = 1
  getLis()
}

const getLis = async () => {
  const params: {
    status: string
  } = {
    status: searchValue.value.status,
  }
  const { result } = await cloud.callFunction({
    name: 'wx_order',
    data: {
      action: 'admin/order/list',
      params: { pageNumber: pagination.value.current, ...params },
    },
  })
  pagination.value.total = result.data.total
  tableList.value = result.data.records
}
const changePagination = (paginations: { current: number; pageSize: number }) => {
  console.log(paginations)
  pagination.value.current = paginations.current
  pagination.value.pageSize = paginations.pageSize
  getLis()
}
onMounted(() => {
  getLis()
})
</script>
